package com.soccertgc.comm
{
	import com.soccertgc.utils.ClassUtils;

	public class CallFailType
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const EXCEPTION:CallFailType					= new CallFailType(0, "EXCEPTION");			// unexpected error
		public static const REQUEST_ERROR:CallFailType				= new CallFailType(1, "REQUEST_ERROR");		// request sent to the server has some error (missing params or sending throw an exception)
		public static const JSON_ERROR:CallFailType					= new CallFailType(2, "JSON_ERROR");		// JSON parsing has failed
		public static const RESPONSE_ERROR:CallFailType				= new CallFailType(3, "RESPONSE_ERROR");	// response stander object has some errors or missing fields
		public static const HTTP_ERROR:CallFailType					= new CallFailType(4, "HTTP_ERROR");		// HTTP error (404, 500 o timeout)
		public static const IO_ERROR:CallFailType					= new CallFailType(5, "IO_ERROR");			// IO error
		public static const SECURITY_ERROR:CallFailType				= new CallFailType(6, "SECURITY_ERROR");	// SECURITY error
		
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<CallFailType>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<CallFailType>();
			_list.push(CallFailType.EXCEPTION);
			_list.push(CallFailType.JSON_ERROR);
			_list.push(CallFailType.RESPONSE_ERROR);
			_list.push(CallFailType.HTTP_ERROR);
			_list.push(CallFailType.IO_ERROR);
			_list.push(CallFailType.SECURITY_ERROR);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:int;
		private var _name:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function CallFailType(id:int, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("CallFailType enum already created!");
			// Init
			_id    = id;
			_name  = name;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():int
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names for all possible the values of CallFailType enumeration
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * Returns all possible the values for the CallFailType enumeration
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a CallFailType by given id 
		 * @param id Id of the card type to find
		 * @return PlayerCallFailType constant for the given id, null if no CallFailType
		 * for the given id has been found
		 */		
		public static function getById(id:int):CallFailType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("CallFailType for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a CallFailType by given name 
		 * @param name Name of the player CallFailType to find
		 * @return PlayerCallFailType constant for the given id, null if no CallFailType
		 * for the given name has been found
		 */		
		public static function getByName(name:String):CallFailType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("CallFailType for name '" + name + "' unknown!");
		}
		
	}
}